/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */

package com.xw.app.generator.util;

import java.sql.Types;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;

/**
 * The Class DataTypeUtil.
 *
 * @author Xi Wei
 */
public class DataTypeUtil {

	private final static Map<Integer,String> javaTypeForSqlTypeMap = Maps.newHashMap();
	static {
		javaTypeForSqlTypeMap.put(Types.TINYINT, "Byte");
		javaTypeForSqlTypeMap.put(Types.SMALLINT, "Short");
		javaTypeForSqlTypeMap.put(Types.INTEGER, "Integer");
		javaTypeForSqlTypeMap.put(Types.BIGINT, "Long");
		javaTypeForSqlTypeMap.put(Types.REAL, "java.math.BigDecimal");
		javaTypeForSqlTypeMap.put(Types.FLOAT, "java.math.BigDecimal");
		javaTypeForSqlTypeMap.put(Types.DOUBLE, "java.math.BigDecimal");
		javaTypeForSqlTypeMap.put(Types.DECIMAL, "java.math.BigDecimal");
		javaTypeForSqlTypeMap.put(Types.NUMERIC, "java.math.BigDecimal");
		javaTypeForSqlTypeMap.put(Types.BIT, "boolean");
		javaTypeForSqlTypeMap.put(Types.CHAR, "String");
		javaTypeForSqlTypeMap.put(Types.VARCHAR, "String");
		javaTypeForSqlTypeMap.put(Types.LONGNVARCHAR, "String");
		javaTypeForSqlTypeMap.put(Types.LONGVARCHAR, "String");
		javaTypeForSqlTypeMap.put(Types.DATE, "java.sql.Date");
		javaTypeForSqlTypeMap.put(Types.TIME, "java.sql.Time");
		javaTypeForSqlTypeMap.put(Types.TIMESTAMP, "java.sql.Timestamp");
		javaTypeForSqlTypeMap.put(Types.CLOB, "String");
		javaTypeForSqlTypeMap.put(Types.BLOB, "java.sql.Blob");
		javaTypeForSqlTypeMap.put(Types.ARRAY, "java.sql.Array");
		javaTypeForSqlTypeMap.put(Types.REF, "java.sql.Ref");
		javaTypeForSqlTypeMap.put(Types.STRUCT, "Object");
		javaTypeForSqlTypeMap.put(Types.JAVA_OBJECT, "Object");		
	}
	
	private static List<String> numberTypeList = Arrays.asList("Long","Integer","Short","Byte","java.math.BigDecimal");
	private static List<String> dateTypeList = Arrays.asList("java.sql.Date","java.sql.Time","java.sql.Timestamp");
	private static List<String> stringTypeList = Arrays.asList("String");
	
	/**
	 * Gets the java type.
	 *
	 * @param sqlType the sql type
	 * @return the java type
	 */
	public static String getJavaType(int sqlType){
		String javaType = javaTypeForSqlTypeMap.get(sqlType);
		if(javaType == null){
			javaType = "Object";
		}
		return javaType;
	}
	
	/**
	 * Checks if is string column.
	 *
	 * @param javaType the java type
	 * @return true, if is string column
	 */
	public static boolean isStringColumn(String javaType){
		if(stringTypeList.contains(javaType)){
			return true;
		}
		return false;
	}
	
	/**
	 * Checks if is number column.
	 *
	 * @param javaType the java type
	 * @return true, if is number column
	 */
	public static boolean isNumberColumn(String javaType){
		if(numberTypeList.contains(javaType)){
			return true;
		}
		return false;
	}
	
	/**
	 * Checks if is date column.
	 *
	 * @param javaType the java type
	 * @return true, if is date column
	 */
	public static boolean isDateColumn(String javaType){
		if(dateTypeList.contains(javaType)){
			return true;
		}
		return false;
	}
	
	
	
}
